﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Gakita.View
{
    /* This class handles the logic for the Particle System */
    class ParticleSystem
    {
        private const int numberOfParticles     =       400;

        private List<Particle> _particles       =       new List<Particle>();
        private Vector2 _position;

        private static Random rNG               =       new Random();

        public ParticleSystem(Vector2 aPosition)
        {
            for (int i = 0; i < numberOfParticles; i++)
            {
                Particle aParticle = GenerateParticle(aPosition);
                _particles.Add(aParticle);
            }
            _position = aPosition;
        }

        private Particle GenerateParticle(Vector2 aPosition)
        {
            Vector2 position = aPosition;
            Vector2 velocity = new Vector2(
                    0.98f * (float)(rNG.NextDouble() * 2 - 1),
                    0.95f * (float)(rNG.NextDouble() * 2 - 1));
            float angle = 0;
            float angularVelocity = 1f * (float)rNG.NextDouble();

            float lifetime = 0.1f + (float)rNG.NextDouble() / 1.6f;

            Particle GeneratedParticle = new Particle(position, velocity, lifetime, angle, angularVelocity);

            return GeneratedParticle;
        }

        public void Draw(SpriteBatch aSpriteBatch, Texture2D aParticleTexture, float aElapsedTime, Camera aCamera,Vector2 aViewPort)
        {
            for (int i = 0; i < _particles.Count; i++)
            {
                Particle tempparticle = _particles[i];

                tempparticle = _particles[i].Update(aElapsedTime);

                _particles[i] = tempparticle;

                Vector2 modelPosition = _particles[i].GetPosition();

                Vector2 viewPosition = aCamera.GetViewPosition(modelPosition.X, modelPosition.Y, aViewPort);

                int particleViewSize = 5;

                Rectangle destinationRectangle = new Rectangle((int)viewPosition.X - particleViewSize / 2,
                                                               (int)viewPosition.Y - particleViewSize / 2, 
                                                               particleViewSize, particleViewSize);

                Rectangle sourceRectangle = new Rectangle(0, 0, aParticleTexture.Width, aParticleTexture.Height);

                Vector2 origin = new Vector2(aParticleTexture.Width / 2, aParticleTexture.Height / 2);

                aSpriteBatch.Draw(aParticleTexture, destinationRectangle, sourceRectangle, _particles[i]._color, 
                                  _particles[i]._angle, origin, SpriteEffects.None, 0f);

                if (_particles[i]._lifeTime <= 0)
                {
                    _particles.RemoveAt(i);
                    i--;
                }

            }

        }
    }
}
